package com.atguigu.gmall.list.listener;

import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.list.service.ListSerivce;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 李旭
 * @date 2021/10/26 10:48
 * @Description:
 *
 *   搜索  上下架相关 监听器类
 */
@Component
public class ListListener {


    @Autowired
    private ListSerivce listSerivce;

    //上架
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_GOODS),
            value = @Queue(value = MqConst.QUEUE_GOODS_UPPER,durable = "true",autoDelete = "false"),
            key = {MqConst.ROUTING_GOODS_UPPER}
    ))
    public void receiverOnSaleMessage(Long skuId, Message message, Channel channel){
        System.out.println("接收到的skuID:" + skuId);
        try {
            //上架 添加ES索引
            listSerivce.onSale(skuId);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            //e.printStackTrace();
        }
    }


    //下架
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_GOODS),
            value = @Queue(value = MqConst.QUEUE_GOODS_LOWER,durable = "true",autoDelete = "false"),
            key = {MqConst.ROUTING_GOODS_LOWER}
    ))
    public void receiverCancelSaleMessage(Long skuId, Message message, Channel channel){
        System.out.println("接收到的skuID:" + skuId);
        try {
            //下架 删除ES索引
            listSerivce.cancelSale(skuId);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            //e.printStackTrace();
        }
    }






}
